﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Xml.Serialization;

// 订单明细类
class OrderDetail
{
    public string ProductName { get; set; }
    public double Price { get; set; }
    public int Quantity { get; set; }

    public OrderDetail() { }

    public OrderDetail(string productName, double price, int quantity)
    {
        ProductName = productName;
        Price = price;
        Quantity = quantity;
    }

    public override string ToString()
    {
        return $"商品名称：{ProductName}，单价：{Price}，数量：{Quantity}";
    }
}

// 订单类
class Order
{
    public int OrderId { get; set; }
    public string Customer { get; set; }
    public List<OrderDetail> Details { get; set; }

    public double TotalAmount => Details.Sum(detail => detail.Price * detail.Quantity);

    public Order() { }

    public Order(int orderId, string customer, List<OrderDetail> details)
    {
        OrderId = orderId;
        Customer = customer;
        Details = details;
    }

    public override bool Equals(object obj)
    {
        var order = obj as Order;
        return order != null && OrderId == order.OrderId;
    }

    public override string ToString()
    {
        return $"订单号：{OrderId}，客户：{Customer}，订单总金额：{TotalAmount}";
    }
}

// 订单服务类
class OrderService
{
    private List<Order> orders = new List<Order>();

    public void AddOrder(Order order)
    {
        if (orders.Contains(order))
        {
            throw new InvalidOperationException("订单已存在，无法重复添加！");
        }
        orders.Add(order);
    }

    public void RemoveOrder(int orderId)
    {
        Order orderToRemove = orders.FirstOrDefault(order => order.OrderId == orderId);
        if (orderToRemove == null)
        {
            throw new InvalidOperationException("订单不存在，无法删除！");
        }
        orders.Remove(orderToRemove);
    }

    public List<Order> QueryOrders(Func<Order, bool> predicate)
    {
        return orders.Where(predicate).OrderBy(order => order.TotalAmount).ToList();
    }

    public void SortOrders(Comparison<Order> comparison)
    {
        orders.Sort(comparison);
    }

    public void Export(string filePath)
    {
        XmlSerializer serializer = new XmlSerializer(typeof(List<Order>));
        using (TextWriter writer = new StreamWriter(filePath))
        {
            serializer.Serialize(writer, orders);
        }
    }

    public void Import(string filePath)
    {
        XmlSerializer serializer = new XmlSerializer(typeof(List<Order>));
        using (TextReader reader = new StreamReader(filePath))
        {
            List<Order> importedOrders = (List<Order>)serializer.Deserialize(reader);
            orders.AddRange(importedOrders);
        }
    }
}

class Program
{
    static void Main(string[] args)
    {
        OrderService orderService = new OrderService();

        while (true)
        {
            Console.WriteLine("请选择操作：");
            Console.WriteLine("1. 添加订单");
            Console.WriteLine("2. 删除订单");
            Console.WriteLine("3. 查询订单");
            Console.WriteLine("4. 导出订单");
            Console.WriteLine("5. 导入订单");
            Console.WriteLine("6. 退出");
            Console.Write("请输入操作编号：");
            string choice = Console.ReadLine();

            switch (choice)
            {
                case "1":
                    AddOrder(orderService);
                    break;
                case "2":
                    RemoveOrder(orderService);
                    break;
                case "3":
                    QueryOrders(orderService);
                    break;
                case "4":
                    ExportOrders(orderService);
                    break;
                case "5":
                    ImportOrders(orderService);
                    break;
                case "6":
                    return;
                default:
                    Console.WriteLine("无效操作，请重新输入！");
                    break;
            }
        }
    }

    static void AddOrder(OrderService orderService)
    {
        // 省略添加订单的代码
    }

    static void RemoveOrder(OrderService orderService)
    {
        // 省略删除订单的代码
    }

    static void QueryOrders(OrderService orderService)
    {
        // 省略查询订单的代码
    }

    static void ExportOrders(OrderService orderService)
    {
        Console.Write("请输入要保存的文件路径：");
        string filePath = Console.ReadLine();
        try
        {
            orderService.Export(filePath);
            Console.WriteLine("订单导出成功！");
        }
        catch (Exception e)
        {
            Console.WriteLine($"订单导出失败：{e.Message}");
        }
    }

    static void ImportOrders(OrderService orderService)
    {
        Console.Write("请输入要导入的文件路径：");
        string filePath = Console.ReadLine();
        try
        {
            orderService.Import(filePath);
            Console.WriteLine("订单导入成功！");
        }
        catch (Exception e)
        {
            Console.WriteLine($"订单导入失败：{e.Message}");
        }
    }
}
